#include "atomic_0813_user_permission_service.h"

// 静态存储用户信息，实际应用中可能需要替换为数据库或其他持久化存储
static std::map<std::string, UserPermissionInput> g_userMap;

int addUser(UserPermissionInput *input, UserPermissionOutput *output)
{
    // 检查输入输出指针有效性
    if (input == nullptr || output == nullptr)
    {
        if (output != nullptr)
        {
            output->authorized = false;
            output->message = "Invalid input or output pointer";
            output->return_type = ATOMIC_SERVICE_ERROR;
        }
        return ATOMIC_SERVICE_ERROR;
    }

    // 初始化输出结构体默认值
    output->authorized = false;
    output->message = "";
    output->return_type = ATOMIC_SERVICE_FAIL;
    output->task_name = "addUser_task_" + input->user_id;
    output->exec_endpoint = "atomic_0813_user_permission_service/V1.0.0/addUser";
    output->creator_id = input->user_id;
    output->input_params = "";
    output->required_services = "";
    output->predecessor_tasks = "";
    output->max_retries = 0;
    output->exec_timeout = 30;
    output->retry_policy = "fixed";
    output->priority = 3;
    output->resource_constraints = "{}";
    output->log_level = 1; // INFO级别
    output->content = "";
    output->module_name = "user_permission";

    try
    {
        // 验证必要参数
        if (input->user_id.empty())
        {
            output->message = "User ID cannot be empty";
            output->content = "Validation failed: user_id is empty";
            return output->return_type;
        }

        if (input->token.empty())
        {
            output->message = "Authentication token cannot be empty";
            output->content = "Validation failed: token is empty";
            return output->return_type;
        }

        // 检查用户是否已存在
        if (g_userMap.find(input->user_id) != g_userMap.end())
        {
            output->message = "User already exists: " + input->user_id;
            output->content = "Duplicate user ID: " + input->user_id;
            return output->return_type;
        }

        // 验证角色合法性（示例：仅允许特定角色）
        const std::set<std::string> validRoles = {"admin", "user", "guest"};
        if (validRoles.find(input->role) == validRoles.end())
        {
            output->message = "Invalid role: " + input->role;
            output->content = "Validation failed: invalid role " + input->role;
            return output->return_type;
        }

        // 存储用户信息
        g_userMap[input->user_id] = *input;

        // 构建输入参数JSON
        output->input_params = "{\"user_id\":\"" + input->user_id + "\","
                             "\"role\":\"" + input->role + "\","
                             "\"action\":\"" + input->action + "\","
                             "\"resource\":\"" + input->resource + "\"}";

        // 设置成功状态
        output->authorized = true;
        output->message = "User added successfully: " + input->user_id;
        output->return_type = ATOMIC_SERVICE_SUCCESS;
        output->content = "User " + input->user_id + " with role " + input->role + " added";

        return output->return_type;
    }
    catch (const std::exception &e)
    {
        output->message = "Internal error: " + std::string(e.what());
        output->content = "Exception occurred: " + std::string(e.what());
        output->return_type = ATOMIC_SERVICE_ERROR;
        return output->return_type;
    }
}